{% extends 'rebs/layouts/base.html' %}

{% block title %}Rebs - Project{% endblock %}

{% block content %}
    {% load humanize mathfilters widget_tweaks %}

    <!-- Start Content-->
    <div class="container-fluid">

        <!-- start page title -->
        {% include 'rebs/partials/content_title.html' with  second_bs="신규 프로젝트" title="토지 정보 관리" %}
        <!-- end page title -->

        <div class="row">
            <form class="form-horizontal p-0 col-12" method="get">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">

                            {% with menu_order='3' %}
                                {% include 'rebs_project/partials/head_menu.html' %}
                            {% endwith %}

                            <div class="row">
                                {% include 'rebs/partials/project_select.html' %}
                                <div class="col-sm-12 col-md-6"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body pb-1">
                        <div class="row">
                            <div class="col mb-3">
                                <!-- Button Group -->
                                <div class="btn-group mb-2">
                                    <button type="button" class="btn btn-light"
                                            onclick="location.href='{% url 'rebs:project:site' %}'">지번 별 토지관리
                                    </button>
                                    <button type="button" class="btn btn-light"
                                            onclick="location.href='{% url 'rebs:project:site-owner' %}'">소유자별 토지관리
                                    </button>
                                    <button type="button" class="btn btn-primary">사업부지 매입관리</button>
                                </div>
                            </div>

                            <form method="post" class="col-12 p-0 needs-validation" novalidate>
                                {% csrf_token %}
                                {{ form.project }}
                                {{ form.register }}
                                {{ form.errors }}
                                <div class="col-12 form-row">
                                    <div class="col-sm-12 col-lg-1 bg-light text-center mb-1">
                                        <div class="col p-0 pt-2 pb-2 m-auto h-100"><strong>매입 계약등록</strong></div>
                                    </div>
                                    <div class="col-sm-12 col-lg-11">
                                        <div class="col-12 form-row mb-1">
                                            <div class="col-12 col-lg-2 mb-1">
                                                {{ form.owner|add_class:"form-control select2"|attr:"data-placeholder:소유자 ❖ 필수 선택 ❖"|attr:"data-toggle=select2 required" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-6 col-lg-2 mb-1">
                                                {{ form.contract_date|add_class:"form-control"|attr:"placeholder:계약체결일"|attr:"data-provide=datepicker data-date-format=yyyy-mm-dd data-date-autoclose=true data-toggle=input-mask data-mask-format=0000-00-00" }}
                                            </div>
                                            <div class="col-8 d-none d-lg-block"></div>

                                            <div class="col-xs-12 col-sm-6 col-lg-2 mb-1">
                                                {{ form.total_prices|add_class:"form-control"|attr:"placeholder:총매매대금" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-8 col-lg-2 mb-1">
                                                {{ form.down_pay1|add_class:"form-control"|attr:"placeholder:1차계약금" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1 m-auto">
                                                <div class="custom-control custom-switch">
                                                    {{ form.down_pay1_is_paid|add_class:"custom-control-input"|attr:"placeholder:1차계약금 지급여부" }}
                                                    <label class="custom-control-label"
                                                           for="{{ form.down_pay1_is_paid.id_for_label }}">1차계약금
                                                        지급여부</label>
                                                </div>
                                            </div>

                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.down_pay2|add_class:"form-control"|attr:"placeholder:2차계약금" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.down_pay2_date|add_class:"form-control"|attr:"placeholder:2차계약금 지급일"|attr:"data-provide=datepicker data-date-format=yyyy-mm-dd data-date-autoclose=true data-toggle=input-mask data-mask-format=0000-00-00" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1 m-auto">
                                                <div class="custom-control custom-switch">
                                                    {{ form.down_pay2_is_paid|add_class:"custom-control-input"|attr:"placeholder:1차계약금 지급여부" }}
                                                    <label class="custom-control-label"
                                                           for="{{ form.down_pay2_is_paid.id_for_label }}">2차계약금
                                                        지급여부</label>
                                                </div>
                                            </div>

                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.inter_pay1|add_class:"form-control"|attr:"placeholder:1차중도금" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.inter_pay1_date|add_class:"form-control"|attr:"placeholder:1차중도금 지급일"|attr:"data-provide=datepicker data-date-format=yyyy-mm-dd data-date-autoclose=true data-toggle=input-mask data-mask-format=0000-00-00" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1 m-auto">
                                                <div class="custom-control custom-switch">
                                                    {{ form.inter_pay1_is_paid|add_class:"custom-control-input"|attr:"placeholder:1차계약금 지급여부" }}
                                                    <label class="custom-control-label"
                                                           for="{{ form.inter_pay1_is_paid.id_for_label }}">1차중도금
                                                        지급여부</label>
                                                </div>
                                            </div>

                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.inter_pay2|add_class:"form-control"|attr:"placeholder:2차중도금" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.inter_pay2_date|add_class:"form-control"|attr:"placeholder:2차중도금 지급일"|attr:"data-provide=datepicker data-date-format=yyyy-mm-dd data-date-autoclose=true data-toggle=input-mask data-mask-format=0000-00-00" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1 m-auto">
                                                <div class="custom-control custom-switch">
                                                    {{ form.inter_pay2_is_paid|add_class:"custom-control-input"|attr:"placeholder:2차중도금 지급여부" }}
                                                    <label class="custom-control-label"
                                                           for="{{ form.inter_pay2_is_paid.id_for_label }}">2차중도금
                                                        지급여부</label>
                                                </div>
                                            </div>

                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.remain_pay|add_class:"form-control"|attr:"placeholder:잔금" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.remain_pay_date|add_class:"form-control"|attr:"placeholder:잔금 지급일"|attr:"data-provide=datepicker data-date-format=yyyy-mm-dd data-date-autoclose=true data-toggle=input-mask data-mask-format=0000-00-00" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1 m-auto">
                                                <div class="custom-control custom-switch">
                                                    {{ form.remain_pay_is_paid|add_class:"custom-control-input"|attr:"placeholder:잔금 지급여부" }}
                                                    <label class="custom-control-label"
                                                           for="{{ form.remain_pay_is_paid.id_for_label }}">잔금
                                                        지급여부</label>
                                                </div>
                                            </div>

                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.acc_bank|add_class:"form-control"|attr:"placeholder:은행" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.acc_number|add_class:"form-control"|attr:"placeholder:계좌번호" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-lg-2 mb-1">
                                                {{ form.acc_owner|add_class:"form-control"|attr:"placeholder:예금주" }}
                                            </div>

                                            <div class="form-group col-xs-12 col-lg-10 mb-1">
                                                {{ form.note|add_class:"form-control"|attr:"placeholder:특이사항"|attr:"rows:2" }}
                                            </div>
                                            {% if request.user.is_superuser or request.user.staffauth.project > '1' %}
                                                <div class="col-6 col-lg-1 text-center m-auto">
                                                    {% if request.GET.id %}<a href="{% url 'rebs:project:site-contract' %}{% if request.GET.page %}?page={{ request.GET.page }}{% endif %}">Reset</a>{% endif %}
                                                </div>
                                            {% endif %}
                                            <div class="col-6 col-lg-{% if request.user.is_superuser or request.user.staffauth.project > '1' %}1{% else %}2{% endif %} text-right m-auto">
                                                {% if request.user.is_superuser or request.user.staffauth.project > '1' %}
                                                    <button type="submit" class="btn btn-{% if request.GET.id %}success{% else %}primary{% endif %}">
                                                        {% if request.GET.id %}변경{% else %}신규{% endif %}등록
                                                    </button>
                                                {% else %}
                                                    <button type="button" class="btn btn-outline-secondary"
                                                            onclick="alert('토지 정보 관리 권한이 없습니다. 관리자에게 문의하여 주십시요.')">조회권한 사용자
                                                    </button>
                                                {% endif %}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>

                            <div class="col-12 table-responsive">
                                <div class="row m-1 mt-3 mb-1">
                                    <div class="col-6 text-success">
                                        매입계약 : {{ total_contract }} 건 | 총 매입면적
                                        : {{ total_cont_area|floatformat:"2"|default:"-"|intcomma }}m<sup>2</sup>
                                        ({{ total_cont_area|mul:"0.3025"|floatformat:"2"|intcomma }}평) 등록
                                    </div>
                                    <div class="col-6 text-right">
                                        <a href="{% url 'excel:sites' %}?project={{ this_project.id }}">
                                            <i class="mdi mdi-file-excel-box"></i> Excel Export
                                            <i class="mdi mdi-download ml-1"></i>
                                        </a>
                                    </div>
                                </div>
                                <table id="scroll-horizontal-datatable"
                                       class="table table-hover table-bordered table-centered table-condensed table-sm w-100 m-0 nowrap">
                                    <thead>
                                    <tr class="bg-nav-pills">
                                        <th class="text-center" rowspan="2" style="width: 5%">No</th>
                                        <th class="text-center" rowspan="2" style="width: 8%">소유자</th>
                                        <th class="text-center" rowspan="2" style="width: 9%">계약체결일</th>
                                        <th class="text-center" colspan="2">총계약 면적(m<sup>2</sup>)</th>
                                        <th class="text-center" rowspan="2" style="width: 9%">총매매대금</th>
                                        <th class="text-center" rowspan="2" style="width: 7%">계약금1</th>
                                        <th class="text-center" rowspan="2" style="width: 6%">지급여부</th>
                                        <th class="text-center" rowspan="2" style="width: 7%">계약금2</th>
                                        <th class="text-center" rowspan="2" style="width: 7%">중도금1</th>
                                        <th class="text-center" rowspan="2" style="width: 7%">중도금2</th>
                                        <th class="text-center" rowspan="2" style="width: 8%">잔금</th>
                                        <th class="text-center" rowspan="2" style="width: 6%">지급여부</th>
                                        <th class="text-center" rowspan="2" scope="col" style="width: 7%">
                                            <a href="javascript: void(0);" class="action-icon">
                                                <i class="mdi mdi-pencil"></i></a>
                                            <a href="javascript: void(0);" class="action-icon">
                                                <i class="mdi mdi-delete"></i></a>
                                        </th>
                                    </tr>
                                    <tr class="bg-dragula">
                                        <th class="text-center" style="width: 7%">(m2)</th>
                                        <th class="text-center" style="width: 7%">(평)</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {% for object in object_list %}
                                        <tr>
                                            <td class="text-center">{{ object.pk }}</td>
                                            <td class="text-center"><a href="{% url 'rebs:project:site-contract' %}?{% if request.GET.page %}page={{ request.GET.page }}&{% endif %}id={{ object.id }}">{{ object.owner }}</a></td>
                                            <td class="text-center">{{ object.contract_date|date:"Y-m-d" }}</td>
                                            {% with ta=total_area.pop %}
                                                <td class="text-right">
                                                    {{ ta|floatformat:2|default:"-"|intcomma }}
                                                </td>
                                                <td class="text-right">
                                                    {{ ta|mul:"0.3025"|floatformat:2|default:"-"|intcomma }}
                                                </td>
                                            {% endwith %}
                                            <td class="text-right">{{ object.total_prices|default:"-"|intcomma }}</td>
                                            <td class="text-right">{{ object.down_pay1|default:"-"|intcomma }}</td>
                                            <td class="text-center {% if object.down_pay1_is_paid %}text-success{% endif %}">
                                                {% if object.down_pay1_is_paid %}완료{% else %}-{% endif %}
                                            </td>
                                            <td class="text-right">{{ object.down_pay2|default:"-"|intcomma }}</td>
                                            <td class="text-right">{{ object.inter_pay1|default:"-"|intcomma }}</td>
                                            <td class="text-right">{{ object.inter_pay2|default:"-"|intcomma }}</td>
                                            <td class="text-right">{{ object.remain_pay|default:"-"|intcomma }}</td>
                                            <td class="text-center {% if object.remain_pay_is_paid %}text-success{% endif %}">
                                                {% if object.remain_pay_is_paid %}완료{% else %}-{% endif %}
                                            </td>
                                            <td class="text-center bg-dragula">
                                                <a href="{% url 'rebs:project:site-contract' %}?{% if request.GET.page %}page={{ request.GET.page }}&{% endif %}id={{ object.id }}" class="action-icon">
                                                    <i class="mdi mdi-pencil"></i></a>
                                                <a href="javascript: site_cont_del('{{ object }}', '{{ object.id }}');" class="action-icon">
                                                    <i class="mdi mdi-delete"></i></a>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        {% if is_paginated %}

                            {# to_first_last :: 맨처음 / 맨끝으로 버튼 사용 여부 #}
                            {# page_class ex:: pagination-rounded pagination-sm pagination-lg justify-content-center justify-content-end #}
                            {% include 'partials/pagination.html' with to_first_last="ok" page_class="pagination-rounded justify-content-end" %}

                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

    </div> <!-- container -->

{% endblock %}

{% block third_party_js %}
    <script>
      window.onload = function () {
        document.getElementById('id_project_sel').setAttribute('onchange', 'submit()')
      }

      function site_cont_del(obj, id) {
        if (confirm('[' + obj + ']\n\n삭제 후 복구할 수 없습니다. 해당 계약 건을 삭제하시겠습니까?')) {
          location.href = "/rebs/project/site-contract-delete/" + id
        } else {
          return
        }
      }
    </script>
{% endblock %}
